home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
OLE2BOOK.ZIP
/
CHAP06.ZIP
/
CHAP06
/
COSCHMOO
/
DOCUMENT.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-15
|
18KB
|
815 lines
/*
* DOCUMENT.CPP
* Component Schmoo Chapter 6
*
* Implementation of the CSchmooDoc derivation of CDocument as
* well as an implementation of CPolylineAdviseSink.
*
* Copyright (c)1993 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Software Design Engineer
* Microsoft Systems Developer Relations
*
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "coschmoo.h"
/*
* CSchmooDoc::CSchmooDoc
* CSchmooDoc::~CSchmooDoc
*
* Constructor Parameters:
* hInst HINSTANCE of the application.
*/
CSchmooDoc::CSchmooDoc(HINSTANCE hInst)
: CDocument(hInst)
{
m_uPrevSize=SIZE_RESTORED;
m_pPL=NULL;
m_pPLAdv=NULL;
m_pIPersistStorage=NULL;
//CHAPTER6MOD
m_pIAdviseSink=NULL;
m_dwConn=0;
//End CHAPTER6MOD
return;
}
CSchmooDoc::~CSchmooDoc(void)
{
//CHAPTER6MOD
LPDATAOBJECT pIDataObject;
HRESULT hr;
//Turn off the advise.
if (NULL!=m_pPL && 0!=m_dwConn)
{
hr=m_pPL->QueryInterface(IID_IDataObject, (LPVOID FAR *)&pIDataObject);
if (SUCCEEDED(hr))
pIDataObject->DUnadvise(m_dwConn);
}
if (NULL!=m_pIAdviseSink)
delete m_pIAdviseSink;
//End CHAPTER6MOD
if (NULL!=m_pIPersistStorage)
m_pIPersistStorage->Release();
if (NULL!=m_pPLAdv)
delete m_pPLAdv;
if (NULL!=m_pPL)
m_pPL->Release();
CoFreeUnusedLibraries();
return;
}
/*
* CSchmooDoc::FInit
*
* Purpose:
* Initializes an already created document window. The client actually
* creates the window for us, then passes that here for further
* initialization.
*
* Parameters:
* pDI LPDOCUMENTINIT containing initialization parameters.
*
* Return Value:
* BOOL TRUE if the function succeeded, FALSE otherwise.
*/
BOOL CSchmooDoc::FInit(LPDOCUMENTINIT pDI)
{
RECT rc;
HRESULT hr;
//CHAPTER6MOD
FORMATETC fe;
LPDATAOBJECT pIDataObject;
//End CHAPTER6MOD
//Change the stringtable range to our customization.
pDI->idsMin=IDS_DOCUMENTMIN;
pDI->idsMax=IDS_DOCUMENTMAX;
//Do default initialization
if (!CDocument::FInit(pDI))
return FALSE;
//Create the Polyline Object via COMPOBJ.DLL functions.
//CHAPTER6MOD
hr=CoCreateInstance(CLSID_Polyline6, NULL, CLSCTX_INPROC_SERVER
, IID_IPolyline6, (LPVOID FAR *)&m_pPL);
//End CHAPTER6MOD
if (FAILED(hr))
return FALSE;
//Initialize the contained Polyline which creates a window.
GetClientRect(m_hWnd, &rc);
InflateRect(&rc, -8, -8);
if (FAILED(m_pPL->Init(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_POLYLINE)))
return FALSE;
//Set up an advise on the Polyline.
m_pPLAdv=new CPolylineAdviseSink((LPVOID)this, (LPUNKNOWN)this);
if (NULL==m_pPLAdv)
return FALSE;
m_pPL->SetAdvise(m_pPLAdv);
//Get the IPersistStorage interface on the object for loads & saves.
hr=m_pPL->QueryInterface(IID_IPersistStorage, (LPVOID FAR *)&m_pIPersistStorage);
if (FAILED(hr))
return FALSE;
//CHAPTER6MOD
/*
* Create an IAdviseSink and send it to the Polyline's IDataObject
* with the clipboard format for the Polyline (as in IPOLY6.H).
*/
//This is a private macro.
SETDefFormatEtc(fe, m_cf, TYMED_HGLOBAL);
m_pIAdviseSink=new CImpIAdviseSink((LPVOID)this, (LPUNKNOWN)this);
if (NULL==m_pIAdviseSink)
return FALSE;
//Set up an advise for the Polyline format
hr=m_pPL->QueryInterface(IID_IDataObject, (LPVOID FAR *)&pIDataObject);
if (FAILED(hr))
return FALSE;
pIDataObject->DAdvise(&fe, ADVF_NODATA, m_pIAdviseSink, &m_dwConn);
pIDataObject->Release();
//End CHAPTER6MOD
return TRUE;
}
//CHAPTER6MOD
//IUnknown interface for all the interfaces we implement in the document
/*
* CSchmooDoc::QueryInterface
* CSchmooDoc::AddRef
* CSchmooDoc::Release
*
* Purpose:
* IUnknown members for the CSchmooDoc implementation.
*/
STDMETHODIMP CSchmooDoc::QueryInterface(REFIID riid, LPVOID FAR *ppv)
{
*ppv=NULL;
//The document is the unknown
if (IsEqualIID(riid, IID_IUnknown))
*ppv=(LPVOID)this;
//Return contained interfaces for others.
if (IsEqualIID(riid, IID_IPolylineAdviseSink6))
*ppv=(LPVOID)m_pPLAdv;
if (IsEqualIID(riid, IID_IAdviseSink))
*ppv=(LPVOID)m_pIAdviseSink;
/*
* If we actually assign an interface to ppv we need to AddRef it
* since we're returning a new pointer.
*/
if (NULL!=*ppv)
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return ResultFromScode(S_FALSE);
}
STDMETHODIMP_(ULONG) CSchmooDoc::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CSchmooDoc::Release(void)
{
/*
* Since CoSchmoo doesn't use documents like Component Objects, this
* doesn't do anything except provide a debugging point.
*/
return --m_cRef;
}
//End CHAPTER6MOD
/*
* CSchmooDoc::FMessageHook
*
* Purpose:
* Processes WM_SIZE for the document so we can resize the Polyline.
*
* Parameters:
* <WndProc Parameters>
* pLRes LRESULT FAR * in which to store the return value
* for the message.
*
* Return Value:
* BOOL TRUE to prevent further processing, FALSE otherwise.
*/
BOOL CSchmooDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam, LRESULT FAR *pLRes)
{
UINT dx, dy;
RECT rc;
if (WM_SIZE==iMsg)
{
//Don't effect the Polyline size to or from minimized state.
if (SIZE_MINIMIZED!=wParam && SIZE_MINIMIZED !=m_uPrevSize)
{
//When we change size, resize any Polyline we hold.
dx=LOWORD(lParam);
dy=HIWORD(lParam);
/*
* If we are getting WM_SIZE in response to a Polyline
* notification, then don't resize the Polyline window again.
*/
if (!m_fNoSize && NULL!=m_pPL)
{
//Resize the polyline to fit the new client
SetRect(&rc, 8, 8, dx-8, dy-8);
m_pPL->RectSet(&rc, FALSE);
/*
* We consider sizing something that makes the file dirty,
* but not until we've finished the create process, which
* is why we set fNoDirty to FALSE in WM_CREATE since we
* get a WM_SIZE on the first creation.
*/
if (!m_fNoDirty)
FDirtySet(TRUE);
SetRect(&rc, 0, 0, dx, dy);
if (NULL!=m_pAdv)
m_pAdv->OnSizeChange((LPCDocument)this, &rc);
m_fNoDirty=FALSE;
}
}
m_uPrevSize=wParam;
}
/*
* We return FALSE even on WM_SIZE so we can let the default procedure
* handle maximized MDI child windows appropriately.
*/
return FALSE;
}
/*
* CSchmooDoc::Clear
*
* Purpose:
* Sets all contents in the document back to defaults with no filename.
*
* Paramters:
* None
*
* Return Value:
* None
*/
void CSchmooDoc::Clear(void)
{
//Completely reset the polyline
m_pPL->New();
CDocument::Clear();
return;
}
/*
* CSchmooDoc::ULoad
*
* Purpose:
* Loads a given document without any user interface overwriting the
* previous contents of the Polyline window. We do this by opening
* the file and telling the Polyline to load itself from that file.
*
* Parameters:
* fChangeFile BOOL indicating if we're to update the window title
*